package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.controller.StorageRecordController;
import com.ktjiaoyu.entity.StorageRecord;
import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.mapper.StorageRecordMapper;
import com.ktjiaoyu.service.StorageRecordService;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StorageRecordServiceImpl implements StorageRecordService {
    private static final Logger LOGGER = LoggerFactory.getLogger(StorageRecordService.class);
    @Autowired
    StorageRecordMapper storageRecordMapper;

    @Override
    public Page<StorageRecord> getPage(StorageRecord storageRecord, Integer pageNo, Integer pageSize) throws Exception {
        Page<StorageRecord> page = null;
        try {
            page = new Page<>();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            //查询总记录数
            Integer total = storageRecordMapper.count(storageRecord);
            page.setTotalCount(total);
            //查询当前页数据
            List<StorageRecord> list = storageRecordMapper.getPage(storageRecord, page.offset(), pageSize);
            page.setData(list);
        } catch (Exception e) {
            LOGGER.error("执行StorageRecordServiceImpl.getPage(storageRecordMapper,pageNo, pageSize)方法出现异常", e);
        }
        return page;
    }

    @Override
    public boolean save(StorageRecord storageRecord) throws Exception {
        try {
            if (storageRecord.getId() == null) {
                return storageRecordMapper.insert(storageRecord) > 0;
            } else {
                return storageRecordMapper.update(storageRecord) > 0;
            }
        } catch (Exception e) {
            LOGGER.error("执行StorageRecordServiceImpl.save(storageRecord)方法出现异常", e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public StorageRecord getById(Integer id) throws Exception {
        return storageRecordMapper.findById(id);
    }

    //根据入库单编号查询入库记录
    @Override
    public StorageRecord getBySrCode(String srCode) {
        return storageRecordMapper.getBySrCode(srCode);
    }

    @Override
    public boolean deleteById(Integer id) {
        return storageRecordMapper.deleteById(id) > 0;
    }
}
